#include "GuiDialog.hpp"

GuiDialog::GuiDialog(DialogManager * dialogManager,float speed)
{
    m_dialogManager = dialogManager;
    status = D_INACTIVE;
    textBufferIndex = 0;
    textSpeed = speed;
    m_dialogFontAnimated = new DialogFontAnimated(22,16,26,26,2);
    if(!textBubbleTexture.loadFromFile("Ressources/sprites/textBubble.png"))
	{
		printf("ERROR -- Can't load the text bubble");
		exit(-1);
	}
	textBubbleSprite.setTexture(textBubbleTexture);
	textBubbleSprite.setPosition(WINDOW_WIDTH/2 - 790/2,+ WINDOW_HEIGHT - 90);

	currentChunkImage.create(790,84,sf::Color(255,255,255));

	nextCharX = 25;
	nextCharY = 15;
}

GuiDialog::~GuiDialog()
{

}

void GuiDialog::displayCurrentDialog()
{
	assert(textBuffer.size() > 0);

	reset();
	status = D_ACTIVE;
}

void GuiDialog::addDialogChunk(std::string chunk)
{
    int index =0;
    std::string finalChunk;
    if(chunk[0] == '/')
    {
        std::cout<<"Command"<<std::endl;
        index = m_dialogManager->getCommandManager()->handleCommand(chunk, 0);
    }
    finalChunk = chunk.substr(index);
    if (finalChunk != "")
    {
        textBuffer.push_back(finalChunk);
    }
}

void GuiDialog::nextChunk()
{
	textBuffer.erase(textBuffer.begin());
	if(textBuffer.size() > 0)
	{
		reset();
		status = D_ACTIVE;
	}
	else
    {
        status = D_INACTIVE;
    }
}

void GuiDialog::spaceInput()
{
    if(isActive())
    {
        if(textBufferIndex >= textBuffer[0].size())//end of the chunk
        {
            status = D_WAITING_INPUT;
            nextChunkExitValue = 2;
            return;
        }
        charTimeDeltaCounter += Core::deltaTime.asSeconds()*10;
    }
    else if(isActiveOrWaiting())
    {
        if(nextChunkExitValue == 1)
            nextChunk();

        if(nextChunkExitValue == 2)
            nextChunkExitValue--;

    }
}

void GuiDialog::update()
{
    if(isActive())
	{
	    if(textBufferIndex >= textBuffer[0].size())//end of the chunk
        {
            status = D_WAITING_INPUT;
            nextChunkExitValue = 1;
            return;
        }
        charTimeDeltaCounter += Core::deltaTime.asSeconds();

		if(charTimeDeltaCounter >= textSpeed)
		{
			charTimeDeltaCounter = 0;

			if(textBuffer[0][textBufferIndex] == ' ')
			{
				setNextCharPosition();
				nextCharX -= 10;
			}
			else if(textBuffer[0][textBufferIndex] == '\n')
            {
                newLine();
            }
            else if(textBuffer[0][textBufferIndex] == '\t')
            {
                nextCharX -=12;
            }
            else if(textBuffer[0][textBufferIndex] == '/')
            {
                textBufferIndex = m_dialogManager->getCommandManager()->handleCommand(textBuffer[0], textBufferIndex);
            }
            else
				m_dialogFontAnimated->printChar(textBuffer[0][textBufferIndex],nextCharX,nextCharY,&currentChunkImage);

			textBufferIndex++;
			setNextCharPosition();
		}
	}
}

void GuiDialog::newLine()
{
    if(nextCharY == 15)
	{
		nextCharY = 15 + m_dialogFontAnimated->getCharHeight() + m_dialogFontAnimated->getLetterSpacing() * 2;
		nextCharX = 25 - m_dialogFontAnimated->getCharWidth() - m_dialogFontAnimated->getLetterSpacing();
	}
}

void GuiDialog::draw(sf::RenderWindow*window)
{
    if(isActiveOrWaiting())
	{
		window->draw(textBubbleSprite);
		currentChunkTexture.loadFromImage(currentChunkImage);
		currentChunkSprite.setTexture(currentChunkTexture);
		currentChunkSprite.setPosition(WINDOW_WIDTH/2 - 790/2,+ WINDOW_HEIGHT - 90);
        window->draw(currentChunkSprite);
	}
}

void GuiDialog::reset()
{
    nextCharX = 25;
	nextCharY = 15;
	textBufferIndex = 0;
	charTimeDeltaCounter = 0;

	currentChunkImage.create(790,84,sf::Color(255,255,255,0));
}

void GuiDialog::setNextCharPosition()
{
	nextCharX += m_dialogFontAnimated->getCharWidth() + m_dialogFontAnimated->getLetterSpacing();
}

